package com.jz.community.moduleshoppingguide.neighbor.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.allen.library.SuperTextView;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.base.ImageBrowserActivity;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.RoundCornersTransformation;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.basecomm.utils.RxImageTool;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.neighbor.bean.BlogsListBean;
import com.jz.community.moduleshoppingguide.neighbor.ui.view.CommentListView;
import com.jz.community.moduleshoppingguide.neighbor.ui.view.ExpandTextView;
import com.jz.community.moduleshoppingguide.neighbor.ui.view.MultiImageView;
import com.jz.community.moduleshoppingguide.neighbor.ui.view.PraiseListView;
import com.jz.community.moduleshoppingguide.neighbor.utils.TimeUtile;
import com.sackcentury.shinebuttonlib.ShineButton;

import java.util.ArrayList;
import java.util.List;

import io.github.rockerhieu.emojiconize.Emojiconize;

public class RelCircleDynamicAdapter extends BaseMultiItemQuickAdapter<BlogsListBean.EmbeddedBean.ContentBean, BaseViewHolder> {


    private OnCircleItemClickListener listener;
    public int bottomHeight = 0;

    public RelCircleDynamicAdapter(@Nullable List<BlogsListBean.EmbeddedBean.ContentBean> data) {
        super(data);
        addItemType(0, R.layout.module_shoppingguide_item_circle_bottom);
        addItemType(1, R.layout.rel_dynamic_circle_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, BlogsListBean.EmbeddedBean.ContentBean item) {
        switch (helper.getItemViewType()) {
            case 0:
                ViewGroup.LayoutParams layoutParams = helper.itemView.getLayoutParams();
                layoutParams.height = bottomHeight;
                break;
            case 1:
                helper.getView(R.id.ll_rel_circle_item).setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onItemClickListener(v, helper.getAdapterPosition(), item);
                    }
                });
                ImageView head_photo = helper.getView(R.id.headIv);
                RoundCornersTransformation transformation = new RoundCornersTransformation(mContext,
                        RxImageTool.dp2px(8), RoundCornersTransformation.CornerType.ALL);
                if (!RxDataTool.isNullString(item.getHeadImg())) {
                    GlideApp.with(mContext)
                            .load(item.getHeadImg())
                            .transform(transformation)
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(head_photo);
                } else {
                    GlideApp.with(mContext)
                            .load(R.mipmap.image_default)
                            .transform(transformation)
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(head_photo);
                }
                head_photo.setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onItemClickHeadImage(v, helper.getAdapterPosition(), item);
                    }
                });
                helper.setText(R.id.nameTv, item.getNickName());
                ExpandTextView content_text = helper.getView(R.id.contentTv);
                Emojiconize.view(content_text).go();
                if (!RxDataTool.isNullString(item.getContent())) {
                    content_text.setVisibility(View.VISIBLE);
                    content_text.setText(item.getContent());
                } else {
                    helper.getView(R.id.contentTv).setVisibility(View.GONE);
                }
                SuperTextView tv_circle_type = helper.getView(R.id.tv_circle_type);
                if (!RxDataTool.isNullString(String.valueOf(item.getBlogType()))) {
                    tv_circle_type.setVisibility(View.VISIBLE);
                    if (item.getBlogType().getId().equals("1")) {
                        tv_circle_type.setCenterString("爆料");
                    } else if (item.getBlogType().getId().equals("2")) {
                        tv_circle_type.setCenterString("求助");
                    } else if (item.getBlogType().getId().equals("3")) {
                        tv_circle_type.setCenterString("二手");
                    }
                } else {
                    tv_circle_type.setVisibility(View.INVISIBLE);
                }
                ShineButton praiseBtn = helper.getView(R.id.iv_praise_btn);
                praiseBtn.setChecked(false);
                if (item.getFavorite() != null && item.getFavorite().size() > 0) {
                    for (int i = 0; i < item.getFavorite().size(); i++) {
                        if (item.getFavorite().get(i).getUserId().equals(BaseSpUtils.getInstance().getUserId(mContext) + "")) {
                            praiseBtn.setChecked(true);
                            break;
                        } else {
                            praiseBtn.setChecked(false);
                        }
                    }
                }
                //点赞按钮
                praiseBtn.setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onItemAddPraiseClick(v, helper.getAdapterPosition(), item);
                    }
                });
                if (!RxDataTool.isNullString(item.getAddTime())) {
                    helper.setText(R.id.timeTv, TimeUtile.putDate(item.getAddTime()));
                }
                MultiImageView multiImageView = helper.getView(R.id.multiImagView);
                String picture = item.getBlogImg();
                List<String> mlist = item.getBlogImgs();
                if (!RxDataTool.isNullString(picture)) {
                    multiImageView.setVisibility(View.VISIBLE);
                    multiImageView.setList(mlist);
                    multiImageView.setOnItemClickListener((view, position) -> {
                        Intent intent = new Intent(mContext, ImageBrowserActivity.class);
                        intent.putStringArrayListExtra("Key_ImageList", (ArrayList<String>) mlist);
                        intent.putExtra("Key_CurrentPosition", position);
                        mContext.startActivity(intent);
                    });
                } else {
                    multiImageView.setVisibility(View.GONE);
                }
                LinearLayout digCommentBody = helper.getView(R.id.digCommentBody);
                if (item.getFavorite() != null && item.getReplyVos() != null) {
                    if (item.getFavorite().size() == 0 && item.getReplyVos().size() == 0) {
                        digCommentBody.setVisibility(View.GONE);
                    } else {
                        digCommentBody.setVisibility(View.VISIBLE);
                    }
                }
                PraiseListView praiseListView = helper.getView(R.id.praiseListView);
                CommentListView commentRV = helper.getView(R.id.rl_commentList);
                if (item.getFavorite() != null && item.getFavorite().size() > 0) {
                    praiseListView.setVisibility(View.VISIBLE);
                    praiseListView.setDatas(item.getFavorite());
                    praiseListView.invalidate();
                } else {
                    praiseListView.setVisibility(View.GONE);
                }
                if (item.getReplyVos() != null && item.getReplyVos().size() > 0) {
                    commentRV.setVisibility(View.VISIBLE);
                    commentRV.setDatas(item.getReplyVos());
                    //点击已评论item
                    commentRV.setOnItemClickListener((v, position) -> {
                        if (listener != null) {
                            listener.onItemPutCommentPosition(helper.itemView, position, helper.getAdapterPosition(), item);
                        }
                    });
                } else {
                    commentRV.setVisibility(View.GONE);
                }
                //点击回复按钮
                helper.getView(R.id.snsBtn).setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onItemAddCommentClick(helper.itemView, helper.getAdapterPosition(), item);
                    }
                });
                break;
        }
    }

    public void setListener(OnCircleItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnCircleItemClickListener {
        void onItemAddPraiseClick(View view, int position, BlogsListBean.EmbeddedBean.ContentBean item);

        void onItemAddCommentClick(View view, int position, BlogsListBean.EmbeddedBean.ContentBean item);

        void onItemPutCommentPosition(View v, int position, int rvPosition, BlogsListBean.EmbeddedBean.ContentBean item);

        void onItemClickListener(View view, int position, BlogsListBean.EmbeddedBean.ContentBean item);

        void onItemClickHeadImage(View view, int position, BlogsListBean.EmbeddedBean.ContentBean item);
    }
}
